package com.example.mobile2021606144ywr0413;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private TextView tvMainTitle;
    private Button btnMainSup;
    private Button btnMainBuy;
    private TextView tvMainResult;
    private Button btnMainClose;
    private String[] parts=new String[]{"轿车引擎","轿车前玻璃","轿车方向盘"};
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        btnMainClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog dialog1;
                AlertDialog.Builder builder1=new AlertDialog.Builder(MainActivity.this);
                builder1.setTitle("普通对话框");
                builder1.setMessage("是否退出应用？");
                builder1.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog1, int which) {
                        dialog1.dismiss();
                        MainActivity.this.finish();
                    }
                });
                builder1.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                dialog1= builder1.create();
                dialog1.show();
            }
        });
        btnMainSup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String[] sup={"国产配件","外资配件"};
                new AlertDialog.Builder(MainActivity.this)
                        .setTitle("请选择供应商")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dialogInterface.dismiss();
                            }
                        })
                        .setSingleChoiceItems(sup, 0, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int i) {
                                tvMainResult.setText(sup[i]);
                            }
                        })
                        .create()
                        .show();
            }
        });
        btnMainBuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog dialog;
                final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("请选择原材料");
                builder.setMultiChoiceItems(parts, null, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                    }
                });
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        tvMainResult.setText(null);
                        for (int i=0;i<parts.length;i++){
                            if (listView.getCheckedItemPositions().get(i)){
                                tvMainResult.append(parts[i]+"\n");
                            }
                        }
                        dialog.dismiss();
                    }
                });
                dialog=builder.create();
                listView=dialog.getListView();
                dialog.show();
            }
        });
    }

    private void initView() {
        tvMainTitle = (TextView) findViewById(R.id.tv_main_title);
        btnMainSup = (Button) findViewById(R.id.btn_main_sup);
        btnMainBuy = (Button) findViewById(R.id.btn_main_buy);
        tvMainResult = (TextView) findViewById(R.id.tv_main_result);
        btnMainClose = (Button) findViewById(R.id.btn_main_close);
    }
}